package com.itheima.service.impl.ums;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.itheima.mapper.ums.UmsAdminMapper;
import com.itheima.pojo.ums.LoginUms;
import com.itheima.pojo.ums.UmsAdmin;
import io.netty.util.internal.ObjectUtil;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.Objects;

@Service
public class UserDetailServiceImpl implements UserDetailsService {

    @Autowired
    private UmsAdminMapper umsAdminMapper;

    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        //查询用户
        LambdaQueryWrapper<UmsAdmin> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(s!=null,UmsAdmin::getUsername,s);
        UmsAdmin umsAdmin = umsAdminMapper.selectOne(lambdaQueryWrapper);
        if (Objects.isNull(umsAdmin)){
            throw new RuntimeException("用户名或者密码错误");
        }

        //TODO 查询用户权限


        //把数据封装成UserDetails对象返回
        return new LoginUms(umsAdmin);
    }
}
